package bus.view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import bus.dao.TicketDao;
import bus.moudle.Admin;
import bus.util.DButil;

public class CancelTicket extends JPanel implements ActionListener{
	JLabel ji1;
	static JTextField jfa1;
	JButton jb2,b1;
	JPanel jp2;
	String s1,s2;
	JFrame jf;
	public String username;
    CancelTicket(String username){
    	this.username=username;
	JButton b1 = new JButton("Search");
	//Button
	jb2=new JButton("Cancel Now");
	jb2.setFont(new java.awt.Font("Dialog",1, 15));
	jb2.setBounds(350,350,150,50);
	
	b1.setFont(new java.awt.Font("Dialog",1, 15));
	b1.setBounds(580,205,100,40);

	//Label
		ji1=new JLabel("Ticket Number:");
		ji1.setBounds(30,120,300,200);
		ji1.setFont(new java.awt.Font("Dialog",1, 25));


	//JTextfield
	jfa1=new JTextField();
		jfa1.setFont(new java.awt.Font("Dialog",1, 15));
	jfa1.setBounds(260,205,300,40);
	jfa1.setBackground(Color.YELLOW);
	
    b1.addActionListener(this);
    jb2.addActionListener(this);

    setVisible(true);
  //JPanel
    setLayout(null);
	add(jfa1);
	add(b1);
	add(ji1);
	add(jb2);
    
}
	@Override
	public void actionPerformed(ActionEvent ae) {
		if(ae.getActionCommand().toString()=="Search")
		{
		Ticket_Info lp=new Ticket_Info(1, username);
		jfa1.setText(lp.search());
		}
		// TODO Auto-generated method stub
		if(ae.getActionCommand().toString()=="Cancel Now")
		{	
			String s1=jfa1.getText().toString();
			 try
		       {
				   DButil db=new DButil();
				   Admin ticket=new Admin();
				   ticket.setTickno(s1);
				   Connection con=db.getCon();
				   TicketDao td=new TicketDao();
				   ResultSet rs=td.ticketSearch(con, ticket);
				   if(rs.next()) {
					    boolean f=td.ticketDEl(con, ticket);
		    			JOptionPane.showConfirmDialog(this, "You have canceled it successfully!",
		    					"Tips Message",JOptionPane.PLAIN_MESSAGE,JOptionPane.WARNING_MESSAGE);
		    	      jfa1.setText("");
				   }
				   else
				   {
						JOptionPane.showConfirmDialog(this, "The information that you input doesn't exist in the database!",
		    					"Tips Message",JOptionPane.PLAIN_MESSAGE,JOptionPane.WARNING_MESSAGE);
				   }
				   
		       }
	
		catch(Exception e)
			 {
			if(s1.length()<=0)
			{
				JOptionPane.showConfirmDialog(this, "The information that you input should not be null!",
						"Tips Message",JOptionPane.PLAIN_MESSAGE,JOptionPane.WARNING_MESSAGE);
			}
		
			}
	
}
	}

}
class trip
{
	public void qie(String li)
	{
	CancelTicket.jfa1.setText(li);
	}
}